package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.ProductionReport;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by maowenjie on 2017/3/14.
 *
 */
@Repository
public class ProductionReportDao extends MyBatisDao<ProductionReport> {
    /**
     *
     * @param id 主 键
     * @return 状态
     */
    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }

    /**
     * 政府端按从日期、到日期和养殖场名称查询本市养殖场生产记录列表
     */
    public Paging<ProductionReport> pagingProductionReports(Integer offset, Integer limit, Map<String, Object> criteria) {
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long) getSqlSession().selectOne(sqlId("GOVCount"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ProductionReport> data = getSqlSession().selectList(sqlId("GovernmentSideQuery"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 养殖端存栏数合计值（只统计当前养殖场已生效，状态写死）
     */
    public Map<String, Integer> aquacultureSum(Map<String ,Object> criteria) {
        Integer changeQuantity = getSqlSession().selectOne(sqlId("SumCombined"), criteria);
        Map<String, Integer> map = Maps.newHashMap();
        if(changeQuantity == null){
            map.put("SumCombined", 0);
        }else {
            map.put("SumCombined", changeQuantity);
        }

        return map;
    }

    /**
     * 按物种统计存栏数合计值
     */
    public List<Map<String , Object>> sumSpeciesStatistics(Map<String ,Object> criteria) {
        return getSqlSession().selectList(sqlId("animalSpecies"),criteria);
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }

    /**
     * 按物种统计存栏数合计值（强大的功能）
     * @param criteria 参数
     * @return Map<String , Object>
     */
    public List<Map<String , Object>> sumChangeQuantityByAnimalType(Map<String,Object> criteria){
        return getSqlSession().selectList(sqlId("sumChangeQuantity"),criteria);
    }

    /**
     * 政府端按从日期、到日期和养殖场名称查询本市养殖场生产记录列表(强大的功能)
     * @param offset 从位置
     * @param limit 多少条
     * @param criteria 参数
     * @return Paging<ProductionReport>
     */
    public Paging<ProductionReport> pagingProductionReportByArea(Integer offset, Integer limit, Map<String,Object> criteria){
        if (criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = this.sqlSession.selectOne(this.sqlId("countByArea"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ProductionReport> data = this.sqlSession.selectList(this.sqlId("pagingByArea"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     *根据时间段查询生产记录
     * @param offset
     * @param limit
     * @param criteria
     * @return Paging<ProductionReport>
     */

    public Paging<ProductionReport> pagingbytimes (Integer offset, Integer limit, Map<String,Object> criteria){

        if (criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = this.sqlSession.selectOne(this.sqlId("countbytimes"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<ProductionReport> data = this.sqlSession.selectList(this.sqlId("pagingbytimes"), criteria);
            return new Paging<>(total, data);
        }


    }

    /**
     *  根据第三种类 分组求和
     * @param criteria 参数
     * @return List<Map<String , Object>>
     */
    public List<Map<String , Object>> countSumByThirdType(Map<String , Object> criteria){
        return getSqlSession().selectList(sqlId("countSumByThirdType"),criteria);
    }

    /**
     * 养殖端的报表
     * @param criteria 参数
     * @return Map<String , Object>
     */
    public Map<String , Object> countSumBIOD(Map<String , Object> criteria){
        return getSqlSession().selectOne(sqlId("countSumBIOD"),criteria);
    }

    /**
     * 查询关联单据号
     * @param relateId
     * @return
     */
    public ProductionReport findProductByRelateId(Long relateId){
        return getSqlSession().selectOne(sqlId("findProductByRelateId"),relateId);
    }

    public Map<String, Object> findSumForWeb(Map<String, Object> map) {

        return getSqlSession().selectOne(sqlId("findSumForWeb"),map);
    }
}